/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package utils.properties;

import constants.Constants;
import constants.PropertyConstants;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Class holds some specific app settings
 *
 * @author rymek
 *         User: Rymek
 *         Date: 24.07.11
 *         Time: 23:21
 */
public class GeneralProperties extends Properties {

    /**
     * Constructor.
     * Also loads properties from a file.
     *
     * @throws IOException thrown when it can't access property files
     */
    public GeneralProperties() throws IOException {
        this.importSettings();
    }

    /**
     * Sets property for Oracle CLient install dir
     *
     * @param oraclePath Oracle Client install dir
     */
    public void setOraclePath(String oraclePath) {
        this.setProperty(PropertyConstants.PROP_ORACLE_CLIENT_PATH, oraclePath);
    }

    /**
     * Gets user defined oracle client install path
     *
     * @return Oracle Client install path
     */
    public String getOraclePath() {
        return this.getProperty(PropertyConstants.PROP_ORACLE_CLIENT_PATH);
    }

    /**
     * Gets database thread limit from property file
     *
     * @param dbThreadLimit limit for database threads
     */
    public void setDatabaseThreadLimit(int dbThreadLimit) {
        this.setProperty(PropertyConstants.PROP_DB_THREAD_LIMIT, Integer.toString(dbThreadLimit));
    }

    /**
     * Gets database thread limit from property file
     *
     * @return database thread limit
     */
    public int getDatabaseThreadLimit() {
        if (Configuration.production) return 1;
        else return Constants.DB_MAX_THREAD_COUNT;
    }

    /**
     * Exports Settings to a file
     *
     * @throws IOException thrown when export fails
     */
    public void exportSettings() throws IOException {

        if (!(new File(PropertyConstants.CFG_CONFIG_DIR)).exists()) {
            if (!new File(PropertyConstants.CFG_CONFIG_DIR).mkdir()) throw new IOException();
        }

        if (!(new File(PropertyConstants.CFG_GENERAL_CFG_FILEPATH)).exists())
            if (!new File(PropertyConstants.CFG_GENERAL_CFG_FILEPATH).createNewFile()) throw new IOException();

        this.store(new FileOutputStream(PropertyConstants.CFG_GENERAL_CFG_FILEPATH), null);
    }

    /**
     * Imports Settings from file
     *
     * @throws IOException thrown when import fails
     */
    void importSettings() throws IOException {

        if ((new File(PropertyConstants.CFG_GENERAL_CFG_FILEPATH)).exists()) {
            this.load(new FileInputStream(PropertyConstants.CFG_GENERAL_CFG_FILEPATH));
        }
    }

}
